Repository navigation
Conversation
calculateBalanceTopMap collected entries in a LinkedList and then copied the sorted entries into the cache with indexed get(i) calls. Each get(i) on a LinkedList is O(n), so with the default unlimited baltop-entry-limit the copy was O(n^2) in the number of users. Plugins that refresh the cache periodically (such as the PlaceholderAPI expansion, every 5 seconds by default) repeat this cost on every refresh. Collect entries in an ArrayList and copy them with an iterator, making the copy O(n) and the whole rebuild O(n log n), bounded by the sort. The sort, tie ordering, entry limit handling and cached data are unchanged.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Information
There was no issue created for this as far as I can tell. I simply just decided to fix it when I noticed the issue in my spark memory allocation report. Basically the issue was that calculateBalanceTopMap allocated a disproportionate amount of memory whenever it refreshed and I discovered it's because it has O(n²) time-complexity and my server has had 300,000 total players.
Details
Proposed fix:
calculateBalanceTopMapcollected entries in aLinkedListand then copied the sorted entries into the cache with indexedget(i)calls. Eachget(i)on aLinkedListis O(n), so with the default unlimitedbaltop-entry-limitthe copy was O(n²) in the number of users. Plugins that refresh the cache periodically (such as the PlaceholderAPI expansion, every 5 seconds by default) repeat this cost on every refresh.This PR collects entries in an
ArrayListand copies them with an iterator, making the copy O(n) and the whole rebuild O(n log n), bounded by the sort. The sort, tie ordering, entry limit handling and cached data are unchanged, and there are no API or config changes.The sorting and limiting logic is moved into a package-private
sortEntrieshelper so it can be unit tested.BalanceTopImplTestcovers ordering, stable ordering of equal balances, entry limits (including-1,0and other negative values, which still produce an empty map as before), empty input, and a 200,000-entryLinkedListthat must finish within 5 seconds. With the old indexedget(i)loop restored, that last test times out.Environments tested:
OS: Fedora Linux 43 (kernel 7.3.6)
Java version: OpenJDK 25.0.3 (Temurin-25.0.3+9)
./gradlew build, all tests and checkstyle pass)Demonstration:
Synthetic benchmark of the sort-and-copy step, old (
LinkedList+get(i)) vs new (ArrayList+ iterator), random balances, average of several runs after warm-up. Both produce identical ordering at every size.Benchmark source: https://gist.github.com/ZepsiZola/18096336efd382bcf44f70de74672a09